<?php
/**
 * 《常见问题》实现类
 *
 * @author 孙晓晔
 * @version $Id: Code.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Faq extends InfoUtil {
	public $tbl = Constant::tbl_faq;

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在审核',
		'3' => '已经发布',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'oper' => '员工',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $_SESSION['id'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = 1 AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['id'] = $row['note_id'];
			$row['text'] = $row['text'];
			
			$row['pos_x'] = intval($row['pos_x']);
			$row['pos_y'] = intval($row['pos_y']);
			$row['width'] = intval($row['width']);
			$row['height'] = intval($row['height']);
			
			$ret[] = $row;
		}
		
		$controller->assign_by_ref('result', json_encode($ret));

		$controller->display($request, 'info/note/layout');
		return true;
	}
	
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/faq/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$name_s = $request['name_s'];

		$where = " WHERE 1=1";

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content '%" . $name_s . "%')";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/faq/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/faq/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = addslashes($request['name']);
		$tag = addslashes($request['tag']);
		$content = addslashes($request['content']);
		
		$oper = $_SESSION['id'];
		$office = $this->_office($oper);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, tag, content, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $tag, $content, $oper);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/faq/edit');
		return true;
	}

	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = addslashes($request['name']);
		$tag = addslashes($request['tag']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET name = '%s', tag = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $tag, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/faq/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
}
?>